import java.util.concurrent.ExecutionException;

import cs2340.spring13.team3.wheresmystuff.R;
import cs2340.spring13.team3.wheresmystuff.presenters.LoginManager;
import cs2340.spring13.team3.wheresmystuff.views.CreateAccountActivity;
import android.test.ActivityInstrumentationTestCase2;
import android.test.UiThreadTest;
import android.widget.Button;
import android.widget.EditText;

/**
 * Testing class to test the functionality of the LoginActivity and the
 * LoginManager
 * 
 * @author Tim Swihart
 * @version 0.1
 * @since 2013-04-06
 */
public class CreateAcctActivityTest extends
		ActivityInstrumentationTestCase2<CreateAccountActivity> {
	private CreateAccountActivity caa;

	public CreateAcctActivityTest(Class<CreateAccountActivity> activityClass) {
		super(CreateAccountActivity.class);
	}
	public CreateAcctActivityTest(){
		super(CreateAccountActivity.class);
	}

	public void setUp() throws Exception {
		super.setUp();
		setActivityInitialTouchMode(false);
		caa = getActivity();// gets current activity
	}
	
	@UiThreadTest
	public void testEverything() {
		assertTrue(getPasswordField()!=null &&
				getPasswordField()!=null &&
				getEmailField()!=null);
		assertTrue(getPasswordField()!=getPasswordRepeat() &&
				getPasswordField()!=getEmailField() &&
				getEmailField()!=getPasswordRepeat());
		checkEmailStuff();
		checkPasswordStuff();
	}

	/**
	 * Checks all email related functionality
	 */
	private void checkEmailStuff() {
		getPasswordField().setText("");
		getPasswordRepeat().setText("");
		checkEmailString("", false, "Field is required");
		checkEmailString("ab.com", false, "Invalid email");
		checkEmailString("a@b", false, "Invalid email");
		checkEmailString("www.com", false, "Invalid email");
		checkEmailString("x@y.com", true, null);
		checkEmailString("pqrs@tuvw.org", true, null);
		checkEmailString("SOMEREEEEEEALLYLONGSTRING@ALOOOOOOOOOOOOOOONGWEBSITE.com", true, null);
	}

	/**
	 * Checks all password related functionality
	 */
	private void checkPasswordStuff() {
		setValidCridentials();
		checkPasswordString("", false, "Field is required");
		checkPasswordString("a", false, "Password too short");
		checkPasswordString("bc", false, "Password too short");
		checkPasswordString("def", false, "Password too short");
		assertTrue(!caa.checkInvalidPassword("ghij"));
		assertTrue(!caa.checkInvalidPassword("something reallllllllllly long"));
		// can't test the text box. It probably ends badly if the activity wants
		// to return.
	}

	private void setValidCridentials() {
		getEmailField().setText("b@c.com");
		getPasswordField().setText("password");
		getPasswordRepeat().setText("password");
	}

	private void checkEmailString(String email, boolean tValue, String error) {
		getEmailField().setText(email);
		System.out.println("email");
		getButton().performClick();
		assertTrue(caa.checkInvalidEmail(email) != tValue);
		if (error == null || error.equals("")) {
			assertTrue(getEmailField().getError() == null
					|| getEmailField().getError().equals(""));
		} else {
			assertTrue(getEmailField().getError().equals(error));
		}
		getEmailField().setError("");
	}

	private void checkPasswordString(String pass, boolean tValue, String error) {
		getPasswordField().setText(pass);
		getButton().performClick();
		getPasswordRepeat().setText("");
		assertTrue(getPasswordRepeat().getError().equals(
				"Password fields do not match"));
		getPasswordRepeat().setText(pass);
		assertTrue(caa.checkInvalidPassword(pass) != tValue);
		if (error == null || error.equals("")) {
			assertTrue(getPasswordField().getError() == null
					|| getPasswordField().getError().equals(""));
		} else {
			assertTrue(getPasswordField().getError().equals(error));
		}
		getPasswordField().setError("");
		getPasswordRepeat().setError("");
	}

	private EditText getEmailField() {
		return (EditText) caa.findViewById(R.id.createAcctEmail);
	}

	private EditText getPasswordField() {
		return (EditText) caa.findViewById(R.id.createAcctPassword);
	}

	private EditText getPasswordRepeat() {
		return (EditText) caa.findViewById(R.id.createAcctPasswordRepeat);
	}

	private Button getButton() {
		return (Button) caa.findViewById(R.id.register_button);
	}

}